import './interfaces';
import { v4 as uuid } from 'uuid';
import send_msg from '@feidao/service/send-msg';

export default function insert(action_id: string, session_id: string, spaceid: string, systemid: string) {
	const argjson = [] as IArgJsonInsert[];

	function prepare<T extends IBaseTable>(tname: string, rowdata: T[]) {
		argjson.push(...rowdata.map((d) => {
			return {
				opflag: 4,
				rowdata: Object.assign({
					_id: uuid()
				}, d),
				tname
			};
		}));
	}

	function exec() {
		const msg = {
			argjson,
			spaceid,
			systemid
		};
		return send_msg(action_id, session_id, spaceid)('dbacc.save.intranet', msg) as Promise<{
			code: number;
			result: string;
		}>;
	}

	return {
		exec,
		prepare
	};
}
